package cn._2dland.uploader;

import java.util.HashMap;
import java.util.Map;

import cn._2dland.uploader.fiftysix.FiftySixClient;
import cn._2dland.uploader.qq.QQClient;
import cn._2dland.uploader.utils.IniFile;
import cn._2dland.uploader.xina.XinaClient;

public class ClientFactory {
	/** ini配置 */
	private static IniFile ini = null;
	/** 客户端映射表 */
	private static Map<String, UploadClient> clients = new HashMap<String, UploadClient>();

	public static void setIniFile(IniFile ini) {
		ClientFactory.ini = ini;
	}
	
	/**
	 * 获取上传客户端实例 
	 * @param type 客户端类型
	 * @return
	 */
	public static UploadClient getUploadClient(String type) {
		UploadClient client = null;

		// 获取账户信息
		String user = ini.getConfigValue(type, "account");
		String pass = ini.getConfigValue(type, "password");

		// 获取客户端
		client = clients.get(type);
		if(client == null) {
			if("xina".equalsIgnoreCase(type)) {
				client = new XinaClient(user, pass);
			} else if("qq".equalsIgnoreCase(type)) {
				client = new QQClient(user, pass);
			} else if("56".equals(type)) {
				client = new FiftySixClient(user, pass);
			} else {
				client = null;
			}
		}
		clients.put(type, client);

		return client;
	}

	/**
	 * 是否支持制定类型的上传
	 * @param type 上传类型
	 * @return
	 */
	public static boolean isSupport(String type) {
		return ("xina".equalsIgnoreCase(type) || 
				"qq".equalsIgnoreCase(type) || 
				"56".equals(type) );
	}
}
